#!/bin/bash
#
# This script builds a fat static library for iOS devices and simulators.
#

set -e

BUILD_FOLDER_DEVICE="build/ios-dev"
BUILD_FOLDER_SIMULATOR="build/ios-sim"
BUILD_FOLDER_CATALYST="build/ios-mac-catalyst"
TARGETS="TrezorCrypto protobuf TrustWalletCore"

function buildDevices() {
    echo -e "\n\nBuilding for iOS Device..."
    mkdir -p "$BUILD_FOLDER_DEVICE"
    cmake -H. -B "$BUILD_FOLDER_DEVICE" -DCMAKE_TOOLCHAIN_FILE=cmake/ios.toolchain.cmake \
                                        -DPLATFORM=OS64 \
                                        -DDEPLOYMENT_TARGET=11.0 \
                                        -DCMAKE_BUILD_TYPE=Release
    make -C "$BUILD_FOLDER_DEVICE" $TARGETS
}

function buildSimulators() {
    echo -e "\nBuilding for iOS Simulator..."
    mkdir -p "$BUILD_FOLDER_SIMULATOR"
    cmake -H. -B "$BUILD_FOLDER_SIMULATOR" -DCMAKE_TOOLCHAIN_FILE=cmake/ios.toolchain.cmake \
                                           -DPLATFORM=SIMULATOR64 \
                                           -DDEPLOYMENT_TARGET=11.0 \
                                           -DCMAKE_BUILD_TYPE=Release
    make -C "$BUILD_FOLDER_SIMULATOR" $TARGETS
}

function buildCatalyst() {
    echo -e "\nBuilding for Mac Catalyst..."
    CATALYST_FLAG="-target x86_64-apple-ios13.2-macabi"
    
    mkdir -p "$BUILD_FOLDER_CATALYST"
    cmake -H. -B "$BUILD_FOLDER_CATALYST" -DCMAKE_TOOLCHAIN_FILE=cmake/ios.toolchain.cmake \
                                          -DPLATFORM=MAC_CATALYST \
                                          -DDEPLOYMENT_TARGET=10.15 \
                                          -DCMAKE_BUILD_TYPE=Release \
                                          -DCMAKE_VERBOSE_MAKEFILE:BOOL=OFF \
                                          -DCMAKE_CXX_FLAGS_MAC_CATALYST:STRING="$CATALYST_FLAG" \
                                          -DCMAKE_C_FLAGS_MAC_CATALYST:STRING="$CATALYST_FLAG" \
                                          -DCMAKE_BUILD_TYPE=MAC_CATALYST
    sed -i '' "s/-mmacosx-version-min=10.15//g" "$BUILD_FOLDER_CATALYST"/trezor-crypto/CMakeFiles/TrezorCrypto.dir/flags.make
    sed -i '' "s/-mmacosx-version-min=10.15//g" "$BUILD_FOLDER_CATALYST"/CMakeFiles/protobuf.dir/flags.make
    sed -i '' "s/-mmacosx-version-min=10.15//g" "$BUILD_FOLDER_CATALYST"/CMakeFiles/TrustWalletCore.dir/flags.make
    make -C "$BUILD_FOLDER_CATALYST" $TARGETS
}

function fattenBinary() {
    echo -e "\n\nFattenning binary..."
    mkdir -p build/ios
    xcrun lipo -create -output build/ios/libprotobuf.a "$BUILD_FOLDER_DEVICE"/libprotobuf.a "$BUILD_FOLDER_SIMULATOR"/libprotobuf.a
    xcrun lipo -create -output build/ios/libTrezorCrypto.a "$BUILD_FOLDER_DEVICE"/trezor-crypto/libTrezorCrypto.a "$BUILD_FOLDER_SIMULATOR"/trezor-crypto/libTrezorCrypto.a
    xcrun lipo -create -output build/ios/libTrustWalletCore.a "$BUILD_FOLDER_DEVICE"/libTrustWalletCore.a "$BUILD_FOLDER_SIMULATOR"/libTrustWalletCore.a
}

function main() {
    buildDevices
    buildSimulators
    fattenBinary
}

main
